#include "Hal/Hal.h"
#include "Page/Page.h"

LV_FONT_DECLARE(myfont);
LV_FONT_DECLARE(pingfang26);
LV_FONT_DECLARE(pingfang30);
LV_IMG_DECLARE(weixin);
LV_IMG_DECLARE(qq);
LV_IMG_DECLARE(wangyiyun);

LV_IMG_DECLARE(spjm);
LV_IMG_DECLARE(wxjm);
LV_IMG_DECLARE(qqjm);
LV_IMG_DECLARE(wyyjm);

#define ACTIVE_LOW true
#define ACTIVE_HIGH false
#define PULLUP_ENABLE true
#define PULLUP_DISABLE false

OneButton btn_push(CONFIG_ENCODER_PUSH_PIN, ACTIVE_LOW, PULLUP_DISABLE);
lv_obj_t *scr_setup, *scr_menu, *scr_page, *home_bg, *menu_bg, *page_bg, *text;
lv_style_t img_bg, bg_style;
uint8_t i, num;
uint8_t up_val, down_val, enter_val, func_index;

void (*current_operation_index)();

void fun1()
{
	lv_scr_load_anim(scr_setup, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
}

void fun2()
{
	lv_scr_load_anim(scr_menu, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
	lv_img_set_src(menu_bg, &weixin);
	lv_label_set_text(text, "#FFFFFF 微信#");
}

void fun3()
{
	lv_scr_load_anim(scr_menu, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
  	lv_img_set_src(menu_bg, &qq);
	lv_label_set_text(text, "#FFFFFF QQ#");
}

void fun4()
{
	lv_scr_load_anim(scr_menu, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
	lv_img_set_src(menu_bg, &wangyiyun);
	lv_label_set_text(text, "#FFFFFF 网易云音乐#");
}

void fun5()
{
	lv_scr_load_anim(scr_page, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
	lv_img_set_src(page_bg, &wxjm);
}

void fun6()
{
	lv_scr_load_anim(scr_page, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
	lv_img_set_src(page_bg, &qqjm);
}

void fun7()
{
	lv_scr_load_anim(scr_page, LV_SCR_LOAD_ANIM_NONE, 50, 0, false);
	lv_img_set_src(page_bg, &wyyjm);
}

typedef struct
{
	uint8_t current;
	uint8_t down; 	//向下翻索引号
	uint8_t enter;	//确认索引号
	uint8_t back; 	//返回索引号
	void (*current_operation)();
}key_table; 

key_table  code_table[10]=
{ 	
	//一级（主界面）
	{0,0,1,0,(*fun1)},
	//二级（菜单栏）
	{1,2,4,0,(*fun2)},
	{2,3,5,0,(*fun3)},
	{3,1,6,0,(*fun4)},
	//三级（子界面）
	{4,4,1,0,(*fun5)},
	{5,5,2,0,(*fun6)},
	{6,6,3,0,(*fun7)},
};

void singleClick()
{
	current_operation_index=code_table[func_index].current_operation;
	(*current_operation_index)();
	func_index=code_table[func_index].down;
}

void doubleClick()
{
	current_operation_index=code_table[func_index].current_operation;
	(*current_operation_index)();
	func_index=code_table[func_index].enter;
}

void longpressstart()
{
	current_operation_index=code_table[func_index].current_operation;
	(*current_operation_index)();
	func_index=code_table[func_index].back;
}

void Page::Init()
{
	btn_push.setDebounceTicks(50); 		//按钮去抖动
	btn_push.setClickTicks(200);		//单击判定时间
	btn_push.attachClick(singleClick);
	btn_push.attachDoubleClick(doubleClick);
	// btn_push.attachMultiClick(multiClick);

	btn_push.setPressTicks(500);		//长按判定时间
	btn_push.attachLongPressStart(longpressstart);
	// btn_push.attachLongPressStop(longpressstop);
	// btn_push.attachDuringLongPress(duringlongpress);

	lv_style_init(&img_bg);
	lv_style_init(&bg_style);
	lv_style_set_bg_color(&bg_style, lv_color_make(0x00, 0x00, 0x00));
	lv_style_set_bg_img_opa(&img_bg, LV_OPA_TRANSP);

	scr_setup = lv_obj_create(NULL);
	scr_menu = lv_obj_create(NULL);
	scr_page = lv_obj_create(NULL);
	lv_obj_add_style(scr_setup, &bg_style, 0);
	lv_obj_add_style(scr_menu, &bg_style, 0);
	lv_obj_add_style(scr_page, &bg_style, 0);
	lv_scr_load_anim(scr_setup, LV_SCR_LOAD_ANIM_NONE, 50, 0, true);

	home_bg = lv_img_create(scr_setup);
	lv_img_set_src(home_bg, &spjm);

    menu_bg = lv_img_create(scr_menu);
	lv_obj_add_style(menu_bg, &img_bg, 0);
	lv_obj_align(menu_bg, LV_ALIGN_TOP_MID, 0, 45);

	text = lv_label_create(scr_menu);
	lv_label_set_recolor(text, true);
	lv_obj_set_style_text_font(text, &pingfang26, 0);
	lv_obj_align(text, LV_ALIGN_BOTTOM_MID, 0, -45);

	page_bg = lv_img_create(scr_page);
}

void Page::Update()
{
	btn_push.tick();
	current_operation_index=code_table[func_index].current_operation; 		
	(*current_operation_index)();//执行当前操作函数
}
